package mortal.game.manager
{
	import Message.Public.*;
	
	import com.gengine.global.*;
	import com.gengine.keyBoard.*;
	
	import extend.js.*;
	
	import flash.events.KeyboardEvent;
	import flash.text.*;
	
	import mortal.common.cd.KeyBoardCd;
	import mortal.common.sound.*;
	import mortal.component.window.*;
	import mortal.game.cache.*;
	import mortal.game.events.*;
	import mortal.game.mvc.*;
	import mortal.game.scene.ai.*;
	import mortal.game.view.common.*;
	import mortal.mvc.core.*;
	import mortal.mvc.interfaces.*;
	
	public class GameManager
	{
		public function GameManager()
		{
			if (_instance != null)
			{
				throw new Error("GameManager 单例");
			}
		}
		
		public function init() : void
		{
			KeyBoardManager.instance.addEventListener(KeyEvent.KEY_DOWN, this.onKeyDownHandler);
			KeyBoardManager.instance.addEventListener(KeyEvent.KEY_UP, this.onKeyUpHandler);
			KeyBoardManager.instance.start();
		}
		
		private function onKeyUpHandler(event:KeyEvent) : void
		{
			var keyCode:* = event.keyEvent.keyCode;
			if (keyCode == KeyCode.SHIFT)
			{
				AIManager.isStopWalk = false;
			}
			else if (keyCode == KeyCode.D)
			{
				KeyBoardCd.keyUp(keyCode);
			}
		}
		
		private function onKeyDownHandler(e:KeyEvent) : void
		{
			
		}
		
		public function popupWindow(target:Object) : void
		{
			/*
			SoundManager.instance.soundPlay(SoundTypeConst.UIclick);
            switch (target)
			{
                case KeyCode.C:
                case ModuleType.Players:
                    this.popupView(GameController.player.view);
                    break;
                case KeyCode.B:
                case ModuleType.Pack:
                    this.popupView(GameController.pack.view);
                    break;
                case KeyCode.V:
                case ModuleType.Skills:
                    this.popupView(GameController.skill.view);
                    break;
                case KeyCode.Q:
                case ModuleType.Tasks:
                    this.popupView(GameController.task.view);
                    break;
                case KeyCode.W:
                case ModuleType.Pets:
                    this.popupView(GameController.pet.view);
                    break;
                case ModuleType.Pay:
                    Dispatcher.dispatchEvent(new DataEvent(EventName.GotoPay));
                    break;
                case KeyCode.G:
                case ModuleType.Guild:
                    if (Cache.instance.guild.hasNewApply){
                        Dispatcher.dispatchEvent(new DataEvent(EventName.OpenGuildWindow, 1));
                        Dispatcher.dispatchEvent(new DataEvent(GuildEventName.OpenGuildApplyListWindow));
                        Cache.instance.guild.hasNewApply = false;
                    } else {
                        this.popupView(GameController.guild.view);
                    };
                    break;
                case ModuleType.Fb:
                    this.popupView(GameController.copy.view);
                    break;
                case ModuleType.Fairyland:
                    if (((!((Cache.instance.role.entityInfo.career == ECareer._ECareerNo))) && (!((Cache.instance.role.entityInfo.career == ECareer._ECareerNewPlayer))))){
                        Dispatcher.dispatchEvent(new DataEvent(EventName.TreasureModuleOpen));
                    } else {
                        if (Cache.instance.role.entityInfo.level < 10){
                            MsgManager.addBroadCast("éè¦å°è¾?0çº§æè½è¿å¥ä»å¢åºæ?");
                        } else {
                            MsgManager.addBroadCast("éè¦è½¬èæè½è¿å¥ä»å¢åºæ?");
                        };
                    };
                    break;
                case KeyCode.J:
                    break;
                case KeyCode.ESCAPE:
                    PopupManager.closeTopWindow();
                    break;
                case KeyCode.F8:
                    JSASSender.instance.callMapResize(1);
                    break;
                case KeyCode.F9:
                case ModuleType.Screen:
                case ModuleType.Show:
                    SystemSetter.currentSetter.hideNearPlayerPet = !(SystemSetter.currentSetter.hideNearPlayerPet);
                    ThingUtil.isEntitySort = true;
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ScreenOrShowPlayer, SystemSetter.currentSetter.hideNearPlayerPet));
                    break;
                case KeyCode.T:
                case ModuleType.Group:
                    this.popupView(GameController.group.view);
                    break;
                case KeyCode.F:
                case ModuleType.Friend:
                    this.popupView(GameController.friend.view);
                    break;
                case KeyCode.ENTER:
                    (GameController.chat.view as ChatModule).setChatInputFocus();
                    KeyBoardManager.instance.changeImeEnable(true);
                    break;
                case KeyCode.K:
                case ModuleType.Build:
                    this.popupView(GameController.equipment.view);
                    break;
                case ENpcEffect._ENpcEffectPlayWeaponCurt:
                    Dispatcher.dispatchEvent(new DataEvent(EventName.Equipment_DoCast_Strengthen, null));
                    break;
                case ENpcEffect._ENpcEffectPlayWeaponPunch:
                    Dispatcher.dispatchEvent(new DataEvent(EventName.Equipment_DoCast_MakeHole, null));
                    break;
                case ENpcEffect._ENpcEffectPlayWeaponRecast:
                    Dispatcher.dispatchEvent(new DataEvent(EventName.Equipment_DoCast_Recast, null));
                    break;
                case ENpcEffect._ENpcEffectPlayWeaponStrengthen:
                    Dispatcher.dispatchEvent(new DataEvent(EventName.Equipment_DoCast_Strengthen, null));
                    break;
                case ENpcEffect._ENpcEffectPlayJewelMosaic:
                    Dispatcher.dispatchEvent(new DataEvent(EventName.Equipment_DoCast_EmbedJewel, null));
                    break;
                case ENpcEffect._ENpcEffectPlayJewelRemoved:
                    Dispatcher.dispatchEvent(new DataEvent(EventName.Equipment_DoCast_RemoveJewel, null));
                    break;
                case ENpcEffect._ENpcEffectPlayJewelSynthesis:
                    Dispatcher.dispatchEvent(new DataEvent(EventName.Equipment_DoCast_ComposeJewel, null));
                    break;
                case ENpcEffect._ENpcEffectPlayPetReqFriend:
                    this.popupView(GameController.petMeet.view);
                    break;
                case ModuleType.PoBox:
                    break;
                case ModuleType.Fukatsu:
                    break;
                case KeyCode.S:
                case ModuleType.Shops:
                    if (GameController.shopMall.view.isHide == true){
                        Dispatcher.dispatchEvent(new DataEvent(EventName.ShopMallOpen));
                    } else {
                        GameController.shopMall.view.hide();
                    };
                    break;
                case ModuleType.WareHouseBuyItem:
                    this.popupView(GameController.wareHouseBuyItem.view);
                    break;
                case ModuleType.WareHouseBuyExtendItem:
                    this.popupView(GameController.wareHouseBuyExtendItem.view);
                    break;
                case KeyCode.SPACE:
                    Dispatcher.dispatchEvent(new DataEvent(EventName.SpaceDropItem));
                    break;
                case KeyCode.ESCAPE:
                    break;
                case ModuleType.Speaker:
                    this.popupView(GameController.speaker.view);
                    break;
                case ModuleType.SysTips:
                    this.popupView(GameController.sysHistory.view);
                    break;
                case KeyCode.O:
                case ModuleType.Activity:
                    this.popupView(GameController.daily.view);
                    break;
                case KeyCode.P:
                case ModuleType.Bang:
                    this.popupView(GameController.ranking.view);
                    break;
                case KeyCode.M:
                case ModuleType.Tu:
                    this.popupView(GameController.bigMap.view);
                    break;
                case ModuleType.Gua:
                    AutoFight.localAutoFight.copy(AutoFight.currentAutoFight);
                    if (AutoFight.localAutoFight.isBrief){
                        this.popupView(GameController.autoFightBrief.autoFightBrief);
                        GameController.autoFightBrief.autoFightComplex.hide();
                    } else {
                        this.popupView(GameController.autoFightBrief.autoFightComplex);
                        GameController.autoFightBrief.autoFightBrief.hide();
                    };
                    break;
                case KeyCode.E:
                case ModuleType.Mail:
                    this.popupView(GameController.mail.view);
                    break;
                case ModuleType.Sound:
                case ModuleType.NoSound:
                    Dispatcher.dispatchEvent(new DataEvent(EventName.BackGroundMusicOpenOrClose));
                    break;
                case ModuleType.Zhu:
                    break;
                case ModuleType.SysSet:
                case ModuleType.She:
                    this.popupView(GameController.systemSetting.view);
                    break;
                case ModuleType.GM:
                    this.popupView(GameController.gm.view);
                    break;
                case ModuleType.FullScreen:
                    JSASSender.instance.callMapResize(1);
                    break;
                case ModuleType.Award:
                    break;
                case ModuleType.Target:
                    this.popupView(GameController.target.view);
                    break;
                case ENpcEffect._ENpcEffectPlayPetAuraUpgrade:
                    this.popupView(GameController.petForge.view);
                    GameController.petForge.setDefaultTab(1);
                    break;
                case ENpcEffect._ENpcEffectPlayPetGrouth:
                    this.popupView(GameController.petForge.view);
                    GameController.petForge.setDefaultTab(0);
                    break;
                case ENpcEffect._ENpcEffectPlayPetReincarnation:
                case ENpcEffect._ENpcEffectPlayPetSkillUpgrade:
                case ENpcEffect._ENpcEffectPlayPetWeaponSynthesis:
                case ENpcEffect._ENpcEffectPass:
                case ENpcEffect._ENpcEffectPlay:
                    break;
                case ENpcEffect._ENpcEffectDrugShop:
                case ENpcEffect._ENpcEffectCopyShop1:
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ShopOpen, {shopCode:this.getShopId(int(_arg1)), shopType:NormalShopType.DrugShop}));
                    break;
                case ENpcEffect._ENpcEffectDefenseShop1:
                case ENpcEffect._ENpcEffectDefenseShop2:
                case ENpcEffect._ENpcEffectDefenseShop3:
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ShopOpen, {shopCode:this.getShopId(int(_arg1)), shopType:NormalShopType.ArmorShop}));
                    break;
                case ENpcEffect._ENpcEffectWeaponShop1:
                case ENpcEffect._ENpcEffectWeaponShop2:
                case ENpcEffect._ENpcEffectWeaponShop3:
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ShopOpen, {shopCode:this.getShopId(int(_arg1)), shopType:NormalShopType.WeaponShop}));
                    break;
                case ENpcEffect._ENpcEffectShop:
                case ENpcEffect._ENpcEffectPetShop:
                case ENpcEffect._ENpcEffectSoulFragmentExchange:
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ShopOpen, {shopCode:this.getShopId(int(_arg1)), shopType:NormalShopType.DefaultShop}));
                    break;
                case ENpcEffect._ENpcEffectItemExchangeShop1:
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ShopOpen, {shopCode:this.getShopId(int(_arg1)), shopType:NormalShopType.ExchangeShop}));
                    break;
                case ENpcEffect._ENpcEffectShopGroceries:
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ShopOpen, {shopCode:this.getShopId(int(_arg1)), shopType:NormalShopType.DefaultShop}));
                    break;
                case ENpcEffect._ENpcEffectPlayStorage:
                    Dispatcher.dispatchEvent(new DataEvent(EventName.StorageOpen));
                    break;
                case ENpcEffect._ENpcEffectTransport:
                    Dispatcher.dispatchEvent(new DataEvent(EventName.TransportInfoReq));
                    break;
                case ENpcEffect._ENpcEffectPlayPetSingleSire:
                    this.popupView(GameController.petForge.view);
                    GameController.petForge.setDefaultTab(2);
                    break;
                case ENpcEffect._ENpcEffectPlayPetGroupSire:
                    Dispatcher.dispatchEvent(new DataEvent(EventName.OpenTeamFeed));
                    break;
                case ENpcEffect._ENpcEffectPlayPetSecondGeneration:
                    Dispatcher.dispatchEvent(new DataEvent(EventName.PetGetBackPet, false));
                    break;
                case ENpcEffect._ENpcEffectSingleCopy:
                case ENpcEffect._ENpcEffectTeamsCopy:
                case ENpcEffect._ENpcEffectPetCopy:
                case ENpcEffect._ENpcEffectCopy1:
                case ENpcEffect._ENpcEffectCopy35:
                case ENpcEffect._ENpcEffectCopy40:
                case ENpcEffect._ENpcEffectCopy111:
                case ENpcEffect._ENpcEffectCopy112:
                case ENpcEffect._ENpcEffectCopy102:
                case ENpcEffect._ENpcEffectCopy114:
                case ENpcEffect._ENpcEffectCopy115:
                case ENpcEffect._ENpcEffectCopy116:
                    Dispatcher.dispatchEvent(new DataEvent(EventName.CopyCopyChooseFromNpc, this.getCopyId(int(_arg1))));
                    break;
                case ENpcEffect._ENpcEffectCopy120:
                    Dispatcher.dispatchEvent(new DataEvent(EventName.SpaShowSpaEntrance));
                    break;
                case ENpcEffect._ENpcEffectExit120:
                    Dispatcher.dispatchEvent(new DataEvent(EventName.SpaLeaveSpa));
                    break;
                case ModuleType.CareerUpdate:
                    this.popupView(GameController.career.view);
                    break;
                case ModuleType.GuideItem:
                    GameController.guideItem.view.show();
                    break;
                case ModuleType.GuideWelcome:
                    GameController.guideWelcome.view.show();
                    break;
                case ENpcEffect._ENpcGuildTask:
                    break;
                case ENpcEffect._ENpcGuildDailyGift:
                    Dispatcher.dispatchEvent(new DataEvent(EventName.GuildGetAward));
                    break;
                case ENpcEffect._ENpcGuildBuyTerritory:
                    Dispatcher.dispatchEvent(new DataEvent(EventName.OpenGuildBuyManorWindow));
                    break;
                case ENpcEffect._ENpcGuildInMyTerritory:
                    Dispatcher.dispatchEvent(new DataEvent(EventName.GuildEnterManor, {guildId:Cache.instance.guild.myGuildInfo.guildId, useContribution:false}));
                    break;
                case ENpcEffect._ENpcGuildInOtherTerritory:
                    Dispatcher.dispatchEvent(new DataEvent(EventName.GuildOpenManorListWindow));
                    break;
                case ENpcEffect._ENpcEffectPetStorage:
                    Dispatcher.dispatchEvent(new DataEvent(EventName.PetStorageOpen));
                    break;
                case ENpcEffect._ENpcGuildMemberMagt:
                    Dispatcher.dispatchEvent(new DataEvent(EventName.OpenGuildWindow, 1));
                    break;
                case ENpcEffect._ENpcGuildBuildInfo:
                    Dispatcher.dispatchEvent(new DataEvent(EventName.OpenGuildWindow, 2));
                    break;
                case ENpcEffect._ENpcGuildShop:
                    Dispatcher.dispatchEvent(new DataEvent(EventName.GuildOpenShop));
                    break;
                case ENpcEffect._ENpcEffectHonsExchange:
                    Dispatcher.dispatchEvent(new DataEvent(EventName.ShopOpenHonor, this.getShopId(int(_arg1))));
                    break;
                case ENpcEffect._ENpcGuildStorage:
                    Dispatcher.dispatchEvent(new DataEvent(EventName.GuildWarehouseOpen));
                    GameController.guildWarehouse.isWarehouseOpenByNpc = true;
                    break;
                case ENpcEffect._ENpcEffectRunBusiness:
                    Dispatcher.dispatchEvent(new DataEvent(EventName.RunBusinessCurrentNpc, Cache.instance.dialog.npcInfo.tnpc.npcId));
                    break;
                case ENpcEffect._ENpcGuildJoin:
                    this.popupView(GameController.guild.view);
                    break;
                case ENpcEffect._ENpcEffectEvilTask:
                    Dispatcher.dispatchEvent(new DataEvent(EventName.KillEvilTaskOpenTaskWin));
                    break;
			}*/
		}
		
		private static var _instance:GameManager = new GameManager;
		public static function get instance() : GameManager
		{
			return _instance;
		}
	}
}